package co.yixiang.modules.system.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.modules.system.annotation.UploadDataListener;
import com.alibaba.excel.EasyExcel;
import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.GuidancePrice;
import co.yixiang.modules.system.service.GuidancePriceService;
import co.yixiang.modules.system.dto.queryCriteria.GuidancePriceQueryCriteria;
import co.yixiang.modules.system.dto.GuidancePriceDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author zf
* @date 2021-07-10
*/
@AllArgsConstructor
@Api(tags = "admin管理")
@RestController
@RequestMapping("/api/guidancePrice")
public class GuidancePriceController {

    private final GuidancePriceService guidancePriceService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','guidancePrice:list')")
    public void download(HttpServletResponse response, GuidancePriceQueryCriteria criteria) throws IOException {
        guidancePriceService.download(Convert.toList(GuidancePriceDto.class, guidancePriceService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询admin")
    @ApiOperation("查询admin")
    @PreAuthorize("@el.check('admin','guidancePrice:list')")
    public ResponseEntity<Object> getGuidancePrices(GuidancePriceQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(guidancePriceService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增admin")
    @ApiOperation("新增admin")
    @PreAuthorize("@el.check('admin','guidancePrice:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody GuidancePrice resources){
        return new ResponseEntity<>(guidancePriceService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改admin")
    @ApiOperation("修改admin")
    @PreAuthorize("@el.check('admin','guidancePrice:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody GuidancePrice resources){
        guidancePriceService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除admin")
    @ApiOperation("删除admin")
    @PreAuthorize("@el.check('admin','guidancePrice:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        guidancePriceService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("导入数据")
    @ApiOperation("导出数据")
    @PostMapping(value = "/import")
    @AnonymousAccess
    public ResponseEntity<Object> imports(@RequestParam("file") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), new UploadDataListener(guidancePriceService)).sheet().doRead();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}